/**
 * 管理端查询订单
 *
 * @project sky-take-out2
 * @author MECHREVO
 * @date 2023/12/12 16:35:09
 * @version 1.0
 */
package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.exception.OrderReceiptException;
import com.sky.mapper.OrderAdminMapper;
import com.sky.result.PageResult;
import com.sky.service.OrderAdminService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service
@Slf4j
public class OrderAdminServiceImpl implements OrderAdminService {
    @Autowired
    private OrderAdminMapper orderAdminMapper;


    @Override
    public PageResult selectConditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
        List<Orders> list = orderAdminMapper.selectConditionSearch(ordersPageQueryDTO);
        System.out.println(list);
        List<OrderVO> orderVOList = new ArrayList<>();
        for (Orders orders : list) {
            OrderVO orderVO = new OrderVO();
            List<OrderDetail> orderDetail= orderAdminMapper.selectOrderDetailByOrderId(orders);
            orderVO.setOrderDetailList(orderDetail);
                BeanUtils.copyProperties(orders,orderVO);
                StringBuilder stringBuilder = new StringBuilder();
            for (OrderDetail detail : orderDetail) {
                stringBuilder.append(detail.getName()).append(" ");
            }
            orderVO.setOrderDishes(stringBuilder.toString());
            orderVOList.add(orderVO);
        }
        Page<Orders> p=(Page<Orders>) list;
        PageResult pageResult=new PageResult(p.getTotal(),orderVOList);
        return pageResult;
    }

    @Override
    public OrderVO selectOrderDetailByOrderId(Long id) {
          Orders orders=orderAdminMapper.selectOrderByOrderId(id);
        OrderVO orderVO = new OrderVO();
        List<OrderDetail> orderDetail= orderAdminMapper.selectOrderDetailByOrderId(orders);
        orderVO.setOrderDetailList(orderDetail);
        BeanUtils.copyProperties(orders,orderVO);
        StringBuilder stringBuilder = new StringBuilder();
        for (OrderDetail detail : orderDetail) {
            stringBuilder.append(detail.getName()).append(" ");
        }
        orderVO.setOrderDishes(stringBuilder.toString());
        return orderVO;
    }

    @Override
    public List<OrderStatisticsVO> selectStatistics() {
       List<Map<String,Object>> listStatus=orderAdminMapper.selectStatistics();
        System.out.println(listStatus);
       /* List<OrderStatisticsVO> orderStatisticsVOS = listStatus.stream()
                .map(item -> {
                    Integer status = (Integer) item.get("status");
                    Long sum = (Long) item.get("sum");
                    OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
                    if (Objects.equals(status, Orders.TO_BE_CONFIRMED)) {
                        orderStatisticsVO.setToBeConfirmed(Math.toIntExact(sum));
                    }else if(Objects.equals(status,Orders.CONFIRMED)){
                        orderStatisticsVO.setConfirmed(Math.toIntExact(sum));
                    }else if(Objects.equals(status,Orders.DELIVERY_IN_PROGRESS)){
                        orderStatisticsVO.setDeliveryInProgress(Math.toIntExact(sum));
                    }
                    return orderStatisticsVO;
                })
                .collect(Collectors.toList());
*/

        OrderStatisticsVO orderStatisticsVO=new OrderStatisticsVO();
        List<OrderStatisticsVO> orderStatisticsVOList=new ArrayList<>();
        for (Map<String, Object> status : listStatus) {
            if(Objects.equals(status.get("status"), Orders.TO_BE_CONFIRMED)){
                orderStatisticsVO.setToBeConfirmed(Math.toIntExact((Long) status.get("sum")));
            }else if(Objects.equals(status.get("status"),Orders.CONFIRMED)){
               orderStatisticsVO.setConfirmed(Math.toIntExact((Long) status.get("sum")));
            }else if(Objects.equals(status.get("status"),Orders.DELIVERY_IN_PROGRESS)){
                orderStatisticsVO.setDeliveryInProgress(Math.toIntExact((Long) status.get("sum")));
            }
            orderStatisticsVOList.add(orderStatisticsVO);
        }
        return orderStatisticsVOList;
    }

    @Override
    public void confirm(OrdersConfirmDTO ordersConfirmDTO) {
        Orders orders = orderAdminMapper.selectOrderByOrderId(ordersConfirmDTO.getId());
        if(orders.getStatus()>=Orders.CONFIRMED){
            throw new OrderReceiptException(MessageConstant.RECEIVED_ORDER);
        }
        orders.setStatus(Orders.CONFIRMED);
        orderAdminMapper.confirm(orders);
    }

    @Override
    public void rejection(OrdersRejectionDTO ordersRejectionDTO) {
        Orders orders = orderAdminMapper.selectOrderByOrderId(ordersRejectionDTO.getId());
        if(orders.getStatus()>=Orders.CONFIRMED){
            throw new OrderReceiptException(MessageConstant.RECEIVED_ORDER);
        }
        orders.setStatus(Orders.CANCELLED);
        orders.setPayStatus(Orders.REFUND);
        orders.setRejectionReason(ordersRejectionDTO.getRejectionReason());
        orderAdminMapper.confirm(orders);
    }

    @Override
    public void cancel(OrdersCancelDTO ordersCancelDTO) {
        Orders orders = orderAdminMapper.selectOrderByOrderId(ordersCancelDTO.getId());
        if(orders.getStatus()<Orders.CONFIRMED){
            throw new OrderReceiptException("订单无法取消");
        }
        orders.setStatus(Orders.CANCELLED);
       orders.setCancelReason(ordersCancelDTO.getCancelReason());
        orders.setPayStatus(Orders.REFUND);
        orderAdminMapper.confirm(orders);
    }

    @Override
    public void delivery(OrdersConfirmDTO ordersConfirmDTO) {
        Orders orders = orderAdminMapper.selectOrderByOrderId(ordersConfirmDTO.getId());
        orders.setStatus(Orders.DELIVERY_IN_PROGRESS);
        orderAdminMapper.confirm(orders);
    }

    @Override
    public void complete(Long id) {
        Orders orders = orderAdminMapper.selectOrderByOrderId(id);
        orders.setStatus(Orders.COMPLETED);
        orderAdminMapper.confirm(orders);
    }
}
